import axios from 'axios';

// import SessionManager from '~/helpers/SessionManager';
// import gotoLogin from '~/utils/gotoLogin';

const baseClientInstance = axios.create({
    baseURL: "http://statistics.xianghuandian.com/statistics.php",   // 记得到时候打开
    timeout: 7000,
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
    },
    // withCredentials:true
});

// 截取请求头
baseClientInstance.interceptors.request.use(
    (config) => {
        console.log(111, config);
        // config.headers.Authorization = `Bearer ${SessionManager.getSessionId()}`;
        return config;
    },
    error => Promise.reject(error),
);


// 截取response返回，自定义返回内容格式及错误返回
baseClientInstance.interceptors.response.use((resp) => {
    const respData = resp.data;
    // console.log('data', resp.data);
    const {
        code,
        msg,
    } = respData;
    // 1000000假定是成功的code.
    if (code !== '2000') {
        switch (code) {
            case '0119':
                alert('用户名已存在');
                break;
            case '0117':
                alert('推荐人号码不存在');
                break;
            case '0115':
                alert('修改失败')
                break;
            case '0116':
                alert('无提现金额')
                break;
            case '0109':
                alert('请确认该用户是否是月卡用户')
                break;
            case '0108':
                alert('请确认该用户是否是已开通')
                break;
            default:
                break;
        }
        // throw new Error(msg || '错误');
        return Promise.reject(respData);
    } else {
        // 输出response值
        return Promise.resolve(respData);
    }
}, (error) => {
    if (error.code === 'ECONNABORTED') throw new Error('请检查您的网络');
    throw error;
});

export default baseClientInstance;